package com.nexus.core.response;

import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * Http消息封装
 * @Date 2025/7/29 17:36
 * @Author luzhengning
 **/
@Data
public class ResponseMessage {
    private int code = 200;
    private Object data;
    private String msg = "SUCCESS";




    //--------------------------------------------------------------------------------------

    public ResponseMessage() {
    }

    public ResponseMessage(int code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    /**
     * 适合接口的 “增删改” 结果处理返回
     * statu: service方法执行结果
     * data: 需要返回的数据，例如执行保存或修改动作后的对象更新
     *
     * @author luzhengning
     * @date 2022/5/12 13:56
     */
    public static ResponseEntity<?> result(boolean statu, Object data) {
        if (statu) {
            return OK(data);
        } else {
            return ERROR("操作失败");
        }
    }

    /**
     * 自定义消息体
     *
     * @author luzhengning
     * @date 2023/3/9 9:11
     */
    public static ResponseEntity<?> body(HttpStatus httpStatus, Object data, String msg) {
        return new ResponseEntity<>(new ResponseMessage(httpStatus.value(),
                data, msg), httpStatus);
    }

    /**
     * 自定义消息体，data=null
     *
     * @author luzhengning
     * @date 2023/3/9 9:12
     */
    public static ResponseEntity<?> body(HttpStatus httpStatus, String msg) {
        return new ResponseEntity<>(new ResponseMessage(httpStatus.value(),
                null, msg), httpStatus);
    }

    /**
     * 常用成功消息体，code=200
     *
     * @author luzhengning
     * @date 2023/3/9 9:12
     */
    public static ResponseEntity<?> OK() {
        return OK(null);
    }

    /**
     * 常用成功消息体，code=200
     *
     * @author luzhengning
     * @date 2023/3/9 9:12
     */
    public static ResponseEntity<?> OK(Object data) {
        return body(HttpStatus.OK, data, "SUCCESS");
    }

    /**
     * 常用成功消息体，code=200
     *
     * @author luzhengning
     * @date 2023/3/9 9:12
     */
    public static ResponseEntity<?> OK(Object data, String msg) {
        return body(HttpStatus.OK, data, msg);
    }

    /**
     * 常用错误消息体，code=500
     *
     * @author luzhengning
     * @date 2023/3/9 9:12
     */
    public static ResponseEntity<?> ERROR(String errorMsg) {
        return ERROR(null, errorMsg);
    }

    /**
     * 常用错误消息体，code=500
     *
     * @author luzhengning
     * @date 2023/3/9 9:12
     */
    public static ResponseEntity<?> ERROR(Object data, String errorMsg) {
        return body(HttpStatus.INTERNAL_SERVER_ERROR, data, errorMsg);
    }

    /**
     * 常用错误消息体，自定义code
     *
     * @author luzhengning
     * @date 2023/3/9 9:12
     */
    public static ResponseEntity<?> ERROR_CODE(int code, String errorMsg) {
        return body(HttpStatus.valueOf(code), errorMsg);
    }

    /**
     * NOT_FOUND，code=404
     *
     * @author luzhengning
     * @date 2023/3/9 9:13
     */
    public static ResponseEntity<?> NOT_FOUND(String errorMsg) {
        return body(HttpStatus.NOT_FOUND, null, errorMsg);
    }

    /**
     * NOT_FOUND，code=404
     *
     * @author luzhengning
     * @date 2023/3/9 9:13
     */
    public static ResponseEntity<?> NOT_FOUND(Object data, String errorMsg) {
        return body(HttpStatus.NOT_FOUND, data, errorMsg);
    }

    /**
     * BAD_REQUEST，code=400
     *
     * @author luzhengning
     * @date 2023/3/9 9:13
     */
    public static ResponseEntity<?> BAD_REQUEST(Object msgData, String errorMsg) {
        return body(HttpStatus.BAD_REQUEST, msgData, errorMsg);
    }

    /**
     * Unauthorized,code=401
     *
     * @author luzhengning
     * @date 2023/3/9 9:13
     */
    public static ResponseEntity<?> Unauthorized(Object msgData, String errorMsg) {
        return body(HttpStatus.UNAUTHORIZED, msgData, errorMsg);
    }

    /**
     * Unauthorized,code=401
     *
     * @author luzhengning
     * @date 2023/3/9 9:14
     */
    public static ResponseEntity<?> Unauthorized(String errorMsg) {
        return body(HttpStatus.UNAUTHORIZED, null, errorMsg);
    }

    /**
     * FORBIDDEN,code=403
     *
     * @author luzhengning
     * @date 2023/3/9 9:13
     */
    public static ResponseEntity<?> Forbidden(Object msgData, String errorMsg) {
        return body(HttpStatus.FORBIDDEN, msgData, errorMsg);
    }

    /**
     * SERVICE_UNAVAILABLE,code=503
     *
     * @author luzhengning
     * @date 2023/3/9 9:13
     */
    public static ResponseEntity<?> SERVICE_UNAVAILABLE(Object msgData, String errorMsg) {
        return body(HttpStatus.SERVICE_UNAVAILABLE, msgData, errorMsg);
    }
}
